使用Loki收集网络设备日志 您所在的位置:网站首页 centos syslog ping 使用Loki收集网络设备日志

使用Loki收集网络设备日志

2023-05-20 06:20| 来源: 网络整理| 查看: 265

0 分享至

用微信扫码二维码

分享至好友和朋友圈

新钛云服已累计为您分享716篇技术干货

前言

Loki是Grafana Labs团队的开源项目,是一个水平可扩展,高可用性,多租户的日志聚合系统,它的设计非常简洁易于操作。

受Prometheus启发的,可以水平扩展、高可用以及支持多租户的日志聚合系统,使用了和Prometheus相同的服务发现机制,将标签添加到日志流中而不是构建全文索引,从promtail接收到的日志和应用的Metrics指标就有相同的标签集,不仅提供了更好的日志和指标之间的上下文切换,还避免了对日志进行全文索引。

日常的网络运维中,如果能把网络设备的日志收集起来,集中查看,在处理故障或日常巡检时,会比较方便一些

如果没有专业的日志收集硬件或软件设备,那么可以搭建Loki进行收集,并使用Grafana查看收集好的日志

本文的主要内容如下:

如何安装部署loki

如何配置网络设备的syslog

如何使用rsyslog收集到网络设备的日志

如何配置Grafana并查看日志

环境准备

1台主机,可以是云主机、虚机,可以根据日志的多少来决定配置的大小,本实验中的配置是4C8G的

OS为Debian11,但其他发行版如CentOS,大部分情况下也是适用的

该主机将会安装loki,rsyslog、promtail

*本文档不再介绍如何安装Grafana

安装部署Loki

下载最新的版本

https://github.com/grafana/loki/releases/[1]

在Linux中安装时可下载loki-linux-amd64.zip

解压可执行文件到目标目录

unzip -d /usr/local/bin/ loki-linux-amd64.zip

创建用户

useradd -r -s /sbin/nologin loki

创建配置文件

mkdir -pv /etc/loki /data/loki

chown -R loki:loki /etc/loki

编辑Loki的配置文件

auth_enabled: false

server:http_listen_port: 3100grpc_listen_port: 9096

common:path_prefix: /data/lokistorage:filesystem:chunks_directory: /data/loki/chunksrules_directory: /data/loki/rulesreplication_factor: 1ring:instance_addr: 10.20.20.20kvstore:store: inmemory

schema_config:configs:- from: 2020-10-24store: boltdb-shipperobject_store: filesystemschema: v11index:prefix: index_period: 24h

ruler:alertmanager_url: http://localhost:9093

请修改以上几项内容

instance_addr,修改为安装主机的IP地址

alertmanager_url:修改为alertmanager的url,本次并未使用alertmanager,所以写的localhost

编辑Systemd[2]的服务配置文件

vim /lib/systemd/system/loki.service

[Unit]Description=Loki serviceAfter=network.target

[Service]Type=simpleUser=lokiExecStart=/usr/local/bin/loki-linux-amd64 -config.file /etc/loki/loki-my-config.yaml

[Install]WantedBy=multi-user.target

启动并设置为开机自启动

systemctl start loki; systemctl enable loki

systemctl status loki

安装并配置promtail

从 https://github.com/grafana/loki/releases[3] 下载安装包

下载示例的配置文件

wget https://raw.githubusercontent.com/grafana/loki/master/cmd/promtail/promtail-local-config.yaml

也可以直接使用如下的示例配置,注意个别地址需要修改,已进行注释

server:http_listen_port: 9080grpc_listen_port: 0

positions:filename: /etc/promtail/positions.yaml

clients:# 将地址修改为实际的 loki Server 的地址- url: http://10.20.20.20:3100/loki/api/v1/push

scrape_configs:- job_name: lokistatic_configs:- targets:- localhostlabels:job: syslogenv: prodlocation: whcqvendor: lokihostname: m-loki__path__: /var/log/network/m-loki-127.0.0.1.log

- job_name: syslogstatic_configs:- targets:- localhostlabels:job: syslogenv: prodlocation: whcq # 设备的机房或者所在的位置vendor: huawei # 品牌hostname: Test-S6720-254 # 主机名__path__: /var/log/network/Test-S6720-254-10.20.99.254.log # 日志的路径

- job_name: syslogstatic_configs:- targets:- localhostlabels:job: syslogenv: prodlocation: shbdvendor: ciscohostname: Test-C3560G__path__: /var/log/network/192.168.99.254-192.168.99.254.log

调整promtail执行文件和配置文件的路径

mv promtail-linux-amd64 /usr/local/bin/

mkdir -pv /etc/promtail; mv promtail-local-config.yaml config-promtail.yml

创建用户并修改文件的权限

useradd -r promtail

chown promtail:promtail /tmp/positions.yaml

编辑 Promtail.service

vim /lib/systemd/system/promtail.service

[Unit]Description=Promtail serviceAfter=network.target

[Service]Type=simpleUser=promtailExecStart=/usr/local/bin/promtail -config.file /etc/promtail/config-promtail.yml

[Install]WantedBy=multi-user.target

启动服务

systemctl start promtail

systemctl enable promtail

使用rsyslog收集到网络设备的日志 · 搭建并配置rsyslog

配置文件如下

vim /etc/rsyslog.conf

# provides UDP syslog receptionmodule(load="imudp")input(type="imudp" port="514")

# provides TCP syslog receptionmodule(load="imtcp")input(type="imtcp" port="514")

$template IpTemplate,"/var/log/network/%HOSTNAME%-%FROMHOST-IP%.log"*.* ?IpTemplate& ~

%HOSTNAME%-%FROMHOST-IP%.log是日志文件的名字,表示主机名+发送源主机的IP

重启服务

systemctl restart rsyslog

配置交换机发送日志到Loki · Cisco交换机# 设置发送日志的源端口logging source-interface Vlan99

# 设置目标主机logging 10.20.20.20· 华为交换机# 根据实际情况修改源接口,或者不配置info-center loghost source Vlanif999

# 设置 syslog 的目标主机info-center loghost 10.20.20.20

# 默认情况下是Info级别,所以此命令可以不执行info-center source default channel loghost log level informational

· 配置Grafana

导入ID为13639的Dashboard

配置Variables,建议的配置如下

3. 查看日志

参考资料:

[1] https://github.com/grafana/loki/releases/: https://github.com/grafana/loki/releases/[2] Systemd: https://www.wolai.com/5YUVEN1zqVdZG2inbWBKh9[3] https://github.com/grafana/loki/releases: https://github.com/grafana/loki/releases

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

/阅读下一篇/ 返回网易首页 下载网易新闻客户端


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有